package minesweeper;

import static java.awt.event.InputEvent.CTRL_DOWN_MASK;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;



import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JLabel;
import java.awt.GridLayout;
import java.awt.FileDialog;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JButton;




public class Minesweeper extends JFrame implements ActionListener, MouseListener {
    public static final String DATE_FORMAT = "yyyy-MM-dd-HH:mm:ss";
    
	private JMenu fileMenu, helpMenu;
	private JMenuItem  options, newGame, saveGame, loadGame, close, about, rules;
	private JTextField customHeight, customWidth, customMines;
	private JButton button;
	private JPanel customPane, buttonPane;
	private JLabel dimension, mine;
	private ButtonGroup group;
	private JFrame OptionFrame, aboutFrame, ruleFrame;
	private JRadioButton beginner, intermediate, expert, custom;	
	// Top Panel 
    private JPanel timerAndFlag;
    private FlagCounter flagC;
    private Cell resetButton;
    private TimerDisplay timer ;
	//
	private int width, height, mines, newAttr;
	private Grid grid;
	
	public Minesweeper(String title) {
		//Main Frame
		super(title);
		JMenuBar menuBar = new JMenuBar(); 
		
		setLayout(new BorderLayout());
		width = 9;
		height = 9;
		mines = 10;
	    grid = new Grid(width,height,mines);

        grid.addMouseListener(this);

        add(grid, BorderLayout.SOUTH);
	   
		timerAndFlag = new JPanel();
        flagC = new FlagCounter(grid.getFlagsLeft());
        resetButton = new Cell();
        timer = new TimerDisplay();
        
        timerAndFlag.add(flagC);
        timerAndFlag.add(resetButton);
        timerAndFlag.add(timer);

        
        add(timerAndFlag, BorderLayout.NORTH);
        
		setBounds(30, 30, 300, 300);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		fileMenu = new JMenu("File");
		fileMenu.setMnemonic('F'); 
		
		newGame = fileMenu.add("New");
		newGame.addActionListener(this);
		newGame.setAccelerator(KeyStroke.getKeyStroke('N', CTRL_DOWN_MASK));
		fileMenu.addSeparator();
		
		saveGame = fileMenu.add("Save");
        saveGame.addActionListener(this);
        saveGame.setAccelerator(KeyStroke.getKeyStroke('S', CTRL_DOWN_MASK));
        
        loadGame = fileMenu.add("Load");
        loadGame.addActionListener(this);
        loadGame.setAccelerator(KeyStroke.getKeyStroke('L', CTRL_DOWN_MASK));
		
		fileMenu.addSeparator();
		
		options = fileMenu.add("Options");
		options.setAccelerator(KeyStroke.getKeyStroke('O', CTRL_DOWN_MASK));
		options.addActionListener(this);
		
		fileMenu.addSeparator();
		
		close = fileMenu.add("Close");
		close.setAccelerator(KeyStroke.getKeyStroke('C', CTRL_DOWN_MASK));
		close.addActionListener(this);
		
		helpMenu = new JMenu("Help");
		helpMenu.setMnemonic('H'); 
		
		rules = helpMenu.add("Rules");
		rules.setAccelerator(KeyStroke.getKeyStroke('R', CTRL_DOWN_MASK));
		rules.addActionListener(this);
		
		helpMenu.addSeparator();
		
		about = helpMenu.add("About");
		about.setAccelerator(KeyStroke.getKeyStroke('A', CTRL_DOWN_MASK));
		about.addActionListener(this);
		
		
		setJMenuBar(menuBar);
		menuBar.add(fileMenu);
		menuBar.add(helpMenu);

		//Option Frame
		OptionFrame = new JFrame("Options");
		
		JPanel contentPane = new JPanel();
		customPane = new JPanel();
		buttonPane = new JPanel();
		
		contentPane.setLayout(new GridLayout(3,5));
		
		beginner = new JRadioButton("Beginner");
		beginner.addActionListener(this);
		
		intermediate = new JRadioButton("intermediate");
		intermediate.setSelected(true);
		intermediate.addActionListener(this);
		
		expert = new JRadioButton("expert");
		expert.addActionListener(this);
		
		custom = new JRadioButton("custom");
		custom.addActionListener(this);
		
		dimension = new JLabel("Deminsions");
		dimension.setVisible(false);
		
		mine = new JLabel("Mines");
		mine.setVisible(false);

		customHeight = new JTextField(3);
		customWidth = new JTextField(3);
		customMines = new JTextField(3);
		customMines.setVisible(false);
		
		customPane.add(customHeight);
		customPane.add(new JLabel("X"));
		customPane.add(customWidth);
		customPane.setVisible(false);
		
		group = new ButtonGroup();
		
		group.add(beginner);
		group.add(intermediate);
		group.add(expert);
		group.add(custom);
		

		button = new JButton("OK!");
		button.addActionListener(this);
		
		OptionFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

		contentPane.add(beginner);
		contentPane.add(intermediate);
		contentPane.add(expert);
		contentPane.add(custom);
		contentPane.add(customPane);
		
		contentPane.add(new JPanel());
		contentPane.add(new JPanel());
		contentPane.add(new JPanel());
		contentPane.add(mine);
		contentPane.add(customMines);
		
		contentPane.add(new JPanel());
		contentPane.add(new JPanel());
		contentPane.add(button);
		contentPane.add(new JPanel());
		contentPane.add(new JPanel());
		
		
		OptionFrame.add(contentPane);
		
		OptionFrame.pack();
		
		//About
		aboutFrame = new JFrame("About");
		aboutFrame.setDefaultCloseOperation(HIDE_ON_CLOSE);
		
		JPanel that = new JPanel();
		
		aboutFrame.pack();
		aboutFrame.setVisible(false);
		aboutFrame.setBounds(30, 30, 300, 300);
		
		//Rules
		ruleFrame = new JFrame("Rules");
		ruleFrame.setDefaultCloseOperation(HIDE_ON_CLOSE);
		JPanel two = new JPanel();
		ruleFrame.pack();
		ruleFrame.setVisible(false);
		ruleFrame.setBounds(30, 30, 300, 300);
		pack();
	}
    public void mouseClicked(MouseEvent e) { 
        if(e.getSource() instanceof Cell){
            Cell currentCell = (Cell)e.getSource();
            if(grid.areMinesPopulated()){
                if(!timer.isStarted()){
                    timer.start();
                }
            }
            flagC.setFlagsLeft(grid.getFlagsLeft());
        }
    }
    public void mouseEntered(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
    }
    public void mousePressed(MouseEvent e) {
    }
    public void mouseReleased(MouseEvent e) {    
    }
	public void actionPerformed(ActionEvent event) {
		
		if(event.getSource() == newGame) {
			remove(grid);
			grid = new Grid(width, height, mines);
			add(grid);
			grid.addMouseListener(this);
			if(timer.isStarted()){
	            timer.reset();  
	            timer.stop();
			} else {
			}
			flagC.setFlagsLeft(grid.getFlagsLeft());
			pack();
			
		} 
		else if (event.getSource() == saveGame){
		    saveGrid();
		} 
		else if (event.getSource() == loadGame){
		    loadGrid();
		}
		else if(event.getSource() == options) {
			setBool(true);
		}	
		else if(event.getSource() == close) {
			setVisible(false);
			System.out.print("Close");
			System.exit(EXIT_ON_CLOSE);
		}			
				
		else if(event.getSource() == about)	{
			aboutFrame.setVisible(true);
		}
		else if(event.getSource() == rules)	{
			ruleFrame.setVisible(true);
		}
		else if(event.getSource() == beginner) {
			customPane.setVisible(false);
			dimension.setVisible(false);
			mine.setVisible(false);
			customMines.setVisible(false);
			
		}		
		else if(event.getSource() == intermediate) {
			customPane.setVisible(false);
			dimension.setVisible(false);	
			mine.setVisible(false);
			customMines.setVisible(false);
		}		
		else if(event.getSource() == expert) {
			customPane.setVisible(false);
			dimension.setVisible(false);	
			mine.setVisible(false);
			customMines.setVisible(false);			
		}	
		else if(event.getSource() == custom) {
			customPane.setVisible(true);
			dimension.setVisible(true);	
			mine.setVisible(true);
			customMines.setVisible(true);
		}
		else if(event.getSource() == button) {
			setBool(false);
			getSelected();
			remove(grid);
			grid = new Grid(width, height, mines);
			add(grid);
			grid.addMouseListener(this);
			if(timer.isStarted()){
	            timer.reset();  
	            timer.stop();
			} else {
			}
			flagC.setFlagsLeft(grid.getFlagsLeft());
			pack();
			
		}	
    
	}

	public void loadGrid(){
		FileDialog load = new FileDialog(OptionFrame);
		load.setVisible(true);
		JFileChooser choose = new JFileChooser("C:\\");
		//FileNameExtensionFilter filter = new FileNameExtensionFilter("Save file *.save", "save");
			 
		//if(filter.accept(choose.getSelectedFile())) {
		
	


	
		try{
			Scanner in = new Scanner(choose.getSelectedFile());
			String s = in.nextLine();
			
			remove(grid);
			grid = new Grid(s);
			add(grid);
			System.out.println(grid);
			grid.addMouseListener(this);
			if(timer.isStarted()){
				timer.reset();  
				timer.stop();
			} else {
			}
			flagC.setFlagsLeft(grid.getFlagsLeft());
			pack();
		} catch(FileNotFoundException e){
		}
		
	}
	public void saveGrid(){
		
		
		//Calendar cal = Calendar.getInstance();
		//SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
		try{
			//PrintWriter out = new PrintWriter(new File(sdf.format(cal + ".save")));
			PrintWriter out = new PrintWriter(new File("game.save"));
			out.print(grid.toString());
			out.close();
		}catch(FileNotFoundException e){
		}
	}
	
	public void setBool(boolean x) {OptionFrame.setVisible(x);}
	public void getDemension(boolean x) {OptionFrame.setVisible(x);}
	public void getSelected() {
		if(beginner.isSelected()) {
			width = 9;
			height = 9; 
			mines = 10;
		}
		else if(intermediate.isSelected()) {
			width = 16;
			height = 16;
			mines = 40;
		}
		else if(expert.isSelected()) {
			width = 30;
			height = 16;
			mines = 99;
		}
		else if(custom.isSelected()) {
			height = Integer.parseInt(customHeight.getText());
			width = Integer.parseInt(customWidth.getText());
			mines = Integer.parseInt(customMines.getText());
			
		}
	}
	public static void main(String[] args) {
	    
		JFrame window = new Minesweeper("Minesweeper");
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setVisible(true);
	}
}